﻿//----------------------------------------------------------------------------------------------------
// The LevelFileInterface is used for saving and laoding of levels
//----------------------------------------------------------------------------------------------------

package HG.Managers
{
	import HG.Debug;
	
	import HG.GameService;
	
	import HG.GameStates.*;
	
	public class StateManager extends IGameManager
	{
		// State ID's
		public static const STATE_NONE:Number = 0;
		public static const STATE_MENU:Number = 1;
		public static const STATE_EDITOR:Number = 2;
		public static const STATE_PLAY:Number = 3;
		public static const STATE_POSTGAME:Number = 4;
		
		var m_states:Array;
		var m_currentState:Number;
		
		//----------------------------------------------------------------------------------------------------
		public function StateManager()
		{
			m_states = new Array();
		}
		
		//----------------------------------------------------------------------------------------------------
		
		//----------------------------------------------------------------------------------------------------
		public override function Initialize()
		{
			// register all states to the state array
			
			m_states[STATE_NONE] = new IGameState();
			m_states[STATE_MENU] = new MenuState();
			m_states[STATE_EDITOR] = new EditorState();
			m_states[STATE_PLAY] = new PlayState();
			m_states[STATE_POSTGAME] = new PostGameState();
			
			m_currentState = STATE_NONE;
		}
		
		//----------------------------------------------------------------------------------------------------
		public override function Shutdown()
		{
			
		}
		
		//----------------------------------------------------------------------------------------------------
		public override function OnTick(DeltaTime:Number)
		{
			if (m_currentState == STATE_NONE)
				SwitchtoState(STATE_MENU);
			
			Debug.ASSERT(m_states[m_currentState], "Attempt to tick to invalid state");
			m_states[m_currentState].OnTick(DeltaTime);
		}
		
		//----------------------------------------------------------------------------------------------------
		public function SwitchtoState(stateID:Number)
		{
			Debug.ASSERT(m_states[stateID], "Attempt to switch to invalid state");
			
			m_states[m_currentState].End();
			
			m_currentState = stateID;
			
			/*var stateName:String = m_states[m_currentState].GetStateName();
			var visualManager = GameService.GetInstance().GetVisualManager();
			visualManager.GetGameArea().gotoAndStop(stateName);*/
			
			m_states[m_currentState].Begin();
		}
		
		//----------------------------------------------------------------------------------------------------

	}

}